require('./nav.css')

var _common_util = require('util/common_util.js');

var _user_service = require('service/user-service.js');
var _cart_service = require('service/cart-service.js')

var nav ={
    init : function(){
        this.onLoad();
        this.bindEvent();
        this.loadUserInfo();
        this.loadCartCount();
        return this;
    },
    onLoad  :   function(){
         // URL绑定
         document.getElementById('cart-link').href = _common_util.getFrontURL('cart.html');
         document.getElementById('order-link').href = _common_util.getFrontURL('order-list.html');
         document.getElementById('user-center-link').href = _common_util.getFrontURL('user-center.html');
         document.getElementById('about-link').href = _common_util.getFrontURL('about.html');
    },
    bindEvent : function (){
        //登录点击事件
        $('.js-login').click(function(){
            _common_util.doLogin();
        });
        //注册点击事件
        $('.js-register').click(function(){
            // window.location.href = './register.html';
            window.location.href = _common_util.getFrontURL('user-register.html');
        });
        //退出点击事件
        $('.js-logout').click(function(){
            // _user_service.logout(
            //     function(res){
            //         window.location.reload();
            //     },
            //     function(errMsg){
            //         _common_util.errorTips(errMsg);
            //     }
            // );
            // _common_util.delCookie("user_id");
            // _common_util.delCookie("username");
            sessionStorage.removeItem("user_id");
            sessionStorage.removeItem("username");
            // window.location.href = this.getFrontURL('user-login.html?redirect=') + encodeURIComponent(window.location.href);
            window.location.reload();
            
        });
    },
    loadUserInfo : function (){
        // _user_service.checkLogin(
        //     function(res){
        //         $('.user.not-login').hide().siblings('.user.login').show().find('.username').text(res.username);
        //     },
        //     function(errMsg){
        //         //do nothing
        //     }
        // );
        // var username = _common_util.getCookie("username");
        var username = sessionStorage.getItem('username');
        if(username){
            $('.user.not-login').hide().siblings('.user.login').show().find('.username').text(username);
            // document.getElementById('cart-link').hide();
            // document.getElementById('order-link').hide();
            // document.getElementById('user-center-link').hide();
        }
        else{
            $('.cart').hide();
            $('.order').hide();
            $('.center').hide();
        }
    },
    // 加载购物车数量
    loadCartCount : function(){
        _cart_service.getCartCount(function(res){
            $('.nav .cart-count').text(res || 0);
        }, function(errMsg){
            $('.nav .cart-count').text(0);
        });
    }
};

module.exports = nav.init();